#!/usr/bin/env python
# encoding: utf-8
"""
    @author:水痕
    @time:2018-08-03 12:42:42:06
    @email:332904234@qq.com
    @version:1.0
    @fileName:email_send.py
    @describe:定义一个发送邮件的工具方法
"""

from .random_str import random_str
from users.models import EmailVerifyRecordModel
from django.core import mail
from django.conf import settings


def send_email(email, sent_type='register'):
    """
    定义一个发送邮件把验证码存入到数据库中
    :param email: 接收的邮箱
    :param send_type: 发送类型
    :return:
    """
    code = random_str(16)
    EmailVerifyRecordModel.objects.create(code=code, email=email, sent_type=sent_type)

    if sent_type == 'register':
        email_title = '欢迎你注册'
        email_body = '请点击下面链接激活你的账号:http://localhost:9000/users/action/{0}'.format(code)

        # send_mail(主题，邮件内容,发送者,接受者)
        send_status = mail.send_mail(email_title, email_body, settings.EMAIL_FROM, [email])
        if send_status:
            pass
        else:
            print('发送失败')
    elif sent_type == 'forget':
        email_title = '修改密码'
        email_body = '请点击下面链接修改你的账号密码:http://localhost:9000/users/updatePasswd/{0}'.format(code)

        # send_mail(主题，邮件内容,发送者,接受者)
        send_status = mail.send_mail(email_title, email_body, settings.EMAIL_FROM, [email])
        if send_status:
            pass
        else:
            print('发送失败')
